package com.android.gufengmall.my;

import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.android.gufengmall.App;
import com.android.gufengmall.BaseActivity;
import com.android.gufengmall.R;
import com.android.gufengmall.adapter.ForumAdapter;
import com.android.gufengmall.bean.ForumBean;
import com.android.gufengmall.bean.UserBean;
import com.android.gufengmall.gen.DaoSession;
import com.android.gufengmall.gen.ForumBeanDao;
import com.android.gufengmall.gen.UserBeanDao;
import com.android.gufengmall.utils.TimeUtils;
import com.bumptech.glide.Glide;

import java.util.List;

/**
 * 论坛
 */
public class ForumActivity extends BaseActivity implements ForumAdapter.IDeleteLongClick {

    private static final String TAG = "ForumActivity";

    private Button mBtnPost;
    private RecyclerView mRvPost;
    private ForumAdapter mForumAdapter;

    private App mMyApp;
    private DaoSession mDaoSession;
    private ForumBeanDao mForumBeanDao;
    private UserBeanDao mUserBeanDao;
    private UserBean mUserBean;

    private List<ForumBean> mForumBeans;
    private String mUserName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forum);

        initView();
        initData();
        initListener();
    }

    private void initListener() {
        mBtnPost.setOnClickListener(v -> {
            View view = LayoutInflater.from(this).inflate(R.layout.item_my_add_forum_dialog, null, false);
            EditText edtTitle = view.findViewById(R.id.edt_title);
            EditText edtInfo = view.findViewById(R.id.edt_info);
            AlertDialog.Builder dialog = new AlertDialog.Builder(this);
            dialog.setTitle(this.getString(R.string.hint));
            dialog.setView(view);
            dialog.setPositiveButton(this.getString(R.string.sure), (dialog1, which) -> {
                String title = edtTitle.getText().toString().trim();
                String info = edtInfo.getText().toString().trim();
                if (title.isEmpty() || info.isEmpty()) {
                    showToast("发布的信息不能为空");
                }
                ForumBean forumBean = new ForumBean();
                forumBean.setTitle(title);
                forumBean.setInfo(info);
                forumBean.setUserName(mUserName);
                forumBean.setDate(TimeUtils.getCurrentSysTime());
                if (mUserBean.getUserPhoto() != null) {
                    forumBean.setUserPhoto(mUserBean.getUserPhoto());
                }
                mForumBeans.add(forumBean);
                mForumBeanDao.insert(forumBean);
                mForumAdapter.notifyDataSetChanged();
                showToast("发布成功");
                dialog1.dismiss();
            });
            dialog.setNegativeButton(this.getString(R.string.cancel), null);
            dialog.create().show();

        });

    }

    private void initData() {
        mUserName = getIntent().getExtras().getString("name");
        mMyApp = (App) this.getApplication();
        mDaoSession = mMyApp.getDaoSession();
        mForumBeanDao = mDaoSession.getForumBeanDao();
        mUserBeanDao = mDaoSession.getUserBeanDao();

        mForumBeans = mForumBeanDao.loadAll();

        List<UserBean> list = mUserBeanDao.queryBuilder().where(UserBeanDao.Properties.UserName.eq(mUserName)).list();
        for (UserBean userBean : list) {
            if (userBean.getUserName().equals(mUserName)) {
                mUserBean = userBean;
            }
        }

        mRvPost.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        mForumAdapter = new ForumAdapter(this, mForumBeans);
        mForumAdapter.setIDeleteLongClick(this);
        mRvPost.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
        mRvPost.setAdapter(mForumAdapter);
    }

    private void initView() {
        mBtnPost = findViewById(R.id.btn_post);
        mRvPost = findViewById(R.id.rv_post);
    }

    @Override
    public void onLongClickDelete(int position) {
        AlertDialog.Builder inputDialog =
                new AlertDialog.Builder(ForumActivity.this);
        inputDialog.setTitle("提示");
        inputDialog.setMessage("确认删除该贴子吗？");
        inputDialog.setPositiveButton("确认", (dialog, which) -> {
            ForumBean forumBean = mForumBeans.get(position);
            mForumBeanDao.delete(forumBean);
            mForumBeans.remove(position);
            mForumAdapter.notifyDataSetChanged();
            showToast("删除成功");
            dialog.dismiss();
        });
        inputDialog.setNegativeButton("取消", null);
        inputDialog.show();
    }

    @Override
    public void onClickForum(int position) {
        Intent intent = new Intent(ForumActivity.this,ForumDetailsActivity.class);
        intent.putExtra("position",position);
        intent.putExtra("userName",mUserName);
        startActivity(intent);
    }
}